class Student:

    def __init__(self, name, age):
        self.__name = name
        self.__age = age

    def study(self, course_name):
        print(f'{self.__name}正在学习{course_name}.')


stu = Student('王大锤', 20)
stu.study('Python程序设计')
# print(stu.__name)  # AttributeError: 'Student' object has no attribute '__name'
print(stu._Student__name)  # AttributeError: 'Student' object has no attribute '__name'

# 增加动态属性
class Student1:
    def __init__(self, name, age):
         self.name = name
         self.age = age
    def study(self):
        print(f'姓名：{self.name}, {self.sex}')

stu1 = Student1('张三', 20)
stu1.sex = '男'

# stu1.study()

# 三角形面积，周长问题
class Triangle:
    """三角形"""

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    # 静态方法
    @staticmethod
    def is_valid(a, b, c):
        return a + b > c and a + c > b and a + c > b
    # @classmethod
    # def is_valid(cls, a, b, c):
    #     """判断三条边长能否构成三角形(类方法)"""
    #     return a + b > c and b + c > a and a + c > b
    # def perimeter(self):
    #     return self.a + self.b + self.c
    # def area(self):
    #     p = self.perimeter() / 2
    #     return (p * (p - self.a) * (p - self.b) * (p - self.c)) ** 0.5
    @property
    def perimeter(self):
        """计算周长"""
        return self.a + self.b + self.c

    @property
    def area(self):
        """计算面积"""
        p = self.perimeter / 2
        return (p * (p - self.a) * (p - self.b) * (p - self.c)) ** 0.5

t = Triangle(3, 4, 5)
# print(f'是不是三角形{t.is_valid(3, 4, 5)}')
# print(f'周长: {t.perimeter()}')
# print(f'面积: {t.area()}')
print(f'是不是三角形{t.is_valid(3, 4, 5)}')
print(f'周长: {t.perimeter}')
print(f'面积: {t.area}')

# 继承和多态

class PersonEx:
    """人"""
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def eat(self):
        print(f'{self.name} 正在吃饭')
    def sleep(self):
        print(f'{self.name} 正在睡觉')

class StudentEx(PersonEx):
    """学生"""
    def __init__(self, name, age):
        super().__init__(name, age)
    def study(self, course_name):
        print(f"{self.name} 正在学习 {course_name} 课程")

class TeacherEx(PersonEx):
    """教师"""
    def __init__(self, name, age, title):
        super().__init__(name, age)
        self.title = title
    def teach(self, course_name):
        print(f"{self.name}{self.title}正在教授{course_name}")

stu2 = StudentEx('白元芳', 21)
stu3 = StudentEx('狄仁杰', 22)
tea1 = TeacherEx('武则天', 35, '副教授')
stu2.eat()
stu3.sleep()
stu2.study("语文")
tea1.eat()
tea1.teach('Python程序设计')